<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="../jquery.min.js"></script>
<script>

	$(function(){
		search();
	});
	
	function search() {
		$.ajax({
			type : "get",
			url : "users",
			dataType : "json",
			data : {
				searchText: $("#searchText").val()
			},
			success : function(result) {
				console.log(result);
				$('#tb').empty();
				/* for(var i=0; i<result.length; i++) {
					result[i].username
				} */
				$.each(result, function(i, d){
					$('#tb').append('<tr><td>'+d.id+'</td><td>'+d.username+'</td><td><a href="javascript:del('+d.id+')">删除</a> <a href="javascript:update()">修改</a></td></tr>');
				});
			}
		});
	}
	
	function add() {
		$.ajax({
			type : "post",
			url : "addUser",
			dataType : "json",
			data : {
				username: $("#username").val()
			},
			success : function(result) {
				if(result) {
					search();
				}else {
					alert('添加失败');
				}
			}
		});
	}
	
	function del(id) {
		$.ajax({
			type : "post",
			url : "delUser",
			dataType : "json",
			data : {
				id: id
			},
			success : function(result) {
				if(result) {
					search();
				}else {
					alert('删除失败');
				}
			}
		});
	}
	
	function update() {
		alert('自行实现')
	}
</script>
</head>
<body>
	<h1>这是登录成功的页面。当前登录者是：${ current_user }</h1>
	<input id="searchText"> <a href="javascript:search()">查找</a>
	<table>
		<thead><tr><td style="width: 80px">id</td><td style="width: 120px">用户名</td><td>操作</td></tr></thead>
		<tbody id="tb"></tbody>
	</table>
	<hr>
	
	<input id="username"> <a href="javascript:add()">添加用户</a>

</body>
</html>